import axios, { AxiosRequestConfig } from "axios";
import qs from "qs";

interface Config extends AxiosRequestConfig {
  token?: string;
  data?: object;
}

export const http = async (
  endpoint: string,
  { data, token, headers, ...customConfig }: Config
) => {
  const config = {
    method: "GET",
    headers: {
      Authorization: token ? `Bearer${token}` : "",
      "Content-Type": data ? "application/json" : "",
    },
    ...customConfig,
  };
  if (config.method.toLocaleLowerCase() === "GET") {
    endpoint += `?${qs.stringify(data)}`;
  } else {
    // config.data = JSON.stringify(data || {})
  }
  return axios(`api/${endpoint}`);
};
